require 'savon'

def urlPAD
   "http://wlsdv.ea.intropro.com:80/Content/Data/PAD/PADService?WSDL"
   "http://soadv.ea.intropro.com:80/soa-infra/services/Content/SharedResources/PADProxy?WSDL"
end

def urlContentMedia
    "http://wlsdv.ea.intropro.com:80/Content/Data/ContentMedia/ContentMediaService?WSDL"
    "http://soadv.ea.intropro.com:80/soa-infra/services/Content/SharedResources/ContentMediaProxy?WSDL"
end

  def medias 
    ['B000002514M3', 'B000002514F3']
    ['B000002514M3', 'B000002514F3', 'B000002516M3', 'B000002516F3', 'B000002517M3', 'B000002517F3', 'B000002518M3', 'B000002518F3', 'B000002519M3', 'B000002519F3', 'B000002524M3']
  end

def time 
  return Time.now.utc.iso8601
end

 def testPADs
  begin
   deletePADs
  t = time
  i=0
  while i<10
	  p "
	  test "+medias.to_s+" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	  "
	  createPADs
  i+=1
  end
  end

  p "start "+t
  p medias
  p "loops count "+i.to_s
  p "end "+time
 end
 
 def deletePADs(medias=medias)
  client = Savon.client(wsdl: urlPAD, namespace: "http://directv.com/amp/das/types")
  
  medias.each_with_index do |media,i| 
  
    message = {enterpriseHeader: {"CreationDateTime"=> time},  programPADFilter: { materialID: media}  }
  
  puts "
  PAD.get_by_filter >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  " + message.to_s + "
  "

  response = client.call(:get_by_filter, message: message)
  
  response.xpath("//id").each{|i| p i.text}

  padsid=[]
  response.xpath("//DEO").each_with_index{|e,i| 
    padsid << {"id"=>  response.xpath("//id")[i].text }
   }
  
  message = {"DEOIdentifier"=> padsid, enterpriseHeader: {"CreationDateTime"=> time}}

  puts "
  PAD.delete >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  " + message.to_s + "
  "

  response = client.call(:delete, message: message)

  response.xpath("//id").each{|i| p i.text}
  
  end
end

def createPADs(medias=medias)

  medias.each_with_index do |media,i| 

    pads = [ {"materialPAD"=>
     {"PADType"=> "" , 
      "offset"=> Time.now.min.to_s + Time.now.sec.to_s + i.to_s , 
      "hex"=> "01451F00002020202020202001180102180018020003E93401410003044900" , 
      "createDate"=> time , 
      "contentIdentifier" => 
	{"materialID"=>media}
     }
    }, 
    {"materialPAD"=>
     {"PADType"=> "" , 
      "offset"=> Time.now.min.to_s + Time.now.sec.to_s + i.to_s , 
      "hex"=> "0146170000202020202020200065FFFFBADD0103044900" , 
      "createDate"=> time , 
      "contentIdentifier" => 
	{"materialID"=>media}
     }
    },
    {"materialPAD"=>
     {"PADType"=> "" , 
      "offset"=> Time.now.min.to_s + Time.now.sec.to_s + i.to_s , 
      "hex"=> "01510F000020202020202020011801" , 
      "createDate"=> time , 
      "contentIdentifier" => 
	{"materialID"=>media}
     }
    }]

    message =  {"DEO"=> pads, enterpriseHeader: {"CreationDateTime"=> time} }

    puts "
    PAD.create >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    " + message.to_s + "
    "
    
    client = Savon.client(wsdl: urlPAD, namespace: "http://directv.com/amp/das/types")
    response = client.call(:create, message: message)

    response.xpath("//id").each{|i| p i.text}

    message = {enterpriseHeader: {"CreationDateTime"=> time}, "DEOIdentifier"=> { "materialID"=> media}}

    puts "
    ContentMedia.get >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    " + message.to_s + "
    "

    client = Savon.client(wsdl: urlContentMedia, namespace: "http://directv.com/amp/das/types")
    response = client.call(:get, message: message)
    
    puts "
    ContentMedia.getResponse.materialPAD.materialID >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    " + response.xpath("//materialPAD//materialID")[0].text + "
    "
  
  end
end

